import java.util.HashSet;
import java.util.Iterator;


public class Automaton implements Cloneable
{
	protected String			name;
	protected HashSet<State>	states;
	
	public Automaton()
	{
		states = new HashSet<State>();
	}
	
	public Automaton(String name)
	{
		states = new HashSet<State>();
		this.name = name;
	}
	
	public Automaton(Automaton a)
	{
		this.name = a.name;
		this.states = new HashSet<State>(a.states);
	}
	
	
	/**
	 * @param states
	 *            the states to set
	 */
	public void setStates(HashSet<State> states)
	{
		this.states = states;
	}
	
	/**
	 * @return the states
	 */
	public HashSet<State> getStates()
	{
		return states;
	}
	
	/**
	 * @return the name
	 */
	public String getName()
	{
		return name;
	}
	
	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	public State getStateById(int id)
	{
		State res = null;
		
		Iterator<State> li = states.iterator();
		boolean fin = false;
		
		while (!fin)
		{
			if (li.hasNext())
			{
				res = li.next();
				
				if (res.getStateId() == id)
				{
					fin = true;
				}
			} else
			{
				fin = true;
				res = null;
			}
		}
		
		return res;
	}
	
	
	/**
	 * @return A string corresponding to the automaton
	 */
	public String toString()
	{
		return name + "\n" + states;
	}
	
	
	protected Object clone()
	{
		Automaton res = new Automaton();
		State temp;
		
		Iterator<State> it = this.states.iterator();
		
		while(it.hasNext())
		{
			temp = it.next();
			res.states.add(temp.copy());
		}
		
		res.name = this.name;
		
		
		return res;
	}
	
	public Automaton copy()
	{
		return (Automaton) this.clone();
	}
}
